import { WidgetBaseTextSession } from './base'
import { toAny, BoolValue, fromAny, StringValue } from '../../proto'
export class WidgetRadioSession extends WidgetBaseTextSession {
    private radio: HTMLInputElement
    private label: HTMLLabelElement
    constructor(render: HTMLElement) {
        super('Radio', render)
    }
    createWidget(): HTMLElement {
        this.radio = document.createElement('input')
        this.radio.type = 'radio'
        this.label = document.createElement('label')
        return this.label
    }
    onCreate(sessionId: number, futureId: number, ...argv) {
        super.onCreate(sessionId, futureId, ...argv);
        this.radio.title = fromAny(argv[4], StringValue)
        this.label.prepend(this.radio)
    }
    onDestroy() {
        this.radio.remove()
        super.onDestroy();
    }
    onReceive(name: String, sessionId: number, futureId: number, ...argv): void {
        switch (name) {
            case 'isChecked':
                return this.isChecked(sessionId, futureId)
            case 'setChecked':
                return this.setChecked(...argv)
        }
        super.onReceive(name, sessionId, futureId, ...argv);
    }
    private setChecked(...argv) {
        this.radio.checked = fromAny(argv[0], BoolValue)
    }
    private isChecked(sessionId: number, futureId: number) {
        const checked = this.radio.checked
        this.sendAction("onIsChecked", sessionId, futureId, toAny(checked, BoolValue))
    }
}